#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonValue>
#include <QDebug>
#include <QFile>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QJsonDocument doc;
    if(doc.isEmpty())
    {
        qDebug() << "json document is emprt ...";
    }
    if(doc.isNull())
    {
        qDebug() << "json document is null ...";
    }
    //writeJson();
    readJson();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::writeJson()//嵌套
{
    QJsonObject obj;
    obj.insert("name","Ace");
    obj.insert("sex","man");
    obj.insert("age",20);
    QJsonObject subObj;
    subObj.insert("father","Gol.D.Roger");
    subObj.insert("mother","Porttags.D.Rouger");
    QJsonArray array;
    array.append("Sabo");
    array.append("Monkey D.luffy");
    subObj.insert("brother",array);
    obj.insert("famliy",subObj);
    obj.insert("isAlive",false);
    obj.insert("comment","yyds");
    /*{
    "age": 20,
    "comment": "yyds",
    "famliy": {
        "brother": [
            "Sabo",
            "Monkey D.luffy"
        ],
        "father": "Gol.D.Roger",
        "mother": "Porttags.D.Rouger"
    },
    "isAlive": false,
    "name": "Ace",
    "sex": "man"
    }*/

    //转换
    QJsonDocument doc(obj);
    QByteArray json = doc.toJson();//转换为文本形式的字符串
    QFile file("D:\\ace.json");
    file.open(QFile::WriteOnly);//打开方式
    file.write(json);
    file.close();
}

void MainWindow::readJson()
{
    QFile file("D:\\ace.json");
    file.open(QFile::ReadOnly);//打开方式
    QByteArray all = file.readAll();
    file.close();
    QJsonDocument doc = QJsonDocument::fromJson(all);
    if(doc.isObject())
    {
        QJsonObject obj = doc.object();
        QStringList keys = obj.keys();
        for(int i = 0;i < keys.size(); ++i)
        {
            QString key = keys.at(i);
            QJsonValue value = obj.value(key);
            if(value.isBool())
            {
                qDebug() << key << ":" << value.toBool();
            }
            else if(value.isString())
            {
                qDebug() << key << ":" << value.toString();
            }
            else if(value.isDouble())
            {
                qDebug() << key << ":" << value.toDouble();
            }
            else if(value.isObject())//对象里面嵌套的对象
            {
                qDebug() << key << ":";
                QJsonObject subObj = value.toObject();//构建一个Json对象
                QStringList subkeys = subObj.keys();
                for(int k = 0;k < subkeys.size(); ++k)
                {
                    QJsonValue subValue = subObj.value(subkeys.at(k));//获取索引k行对应的值
                    if(subValue.isString())//键值对字符串
                    {
                        qDebug() << " " << subkeys.at(k) << ":" << subValue.toString();
                    }
                    else if(subValue.isArray())//嵌套的对象里的数组
                    {
                        QJsonArray array = subValue.toArray();
                        for(int j = 0;j < array.size(); ++j)
                        {
                            qDebug() << " " << array[j].toString();
                        }
                    }
                }
            }
        }
    }
}

